package com.example.demo.controller;

import com.example.demo.domain.User;
import com.example.demo.domain.Wx;
import com.example.demo.service.UserService;
import com.example.demo.utils.BooleanTF;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //向user_user表插入信息
    @PostMapping("/insertUser")
    public boolean insertSelective(@RequestBody User record) {
        int res = userService.insertSelective(record);
        return BooleanTF.judge(res);//调用自定义工具类的方法，判断是否插入成功
    }

    //向user_wx表插入信息
    @PostMapping("/insertWx")
    public boolean insertWx(@RequestBody Wx recode) {
        int res = userService.insertWx(recode);
        return BooleanTF.judge(res);
    }

    @DeleteMapping("/delete/{id}")
    public boolean deleteByPrimaryKey(@PathVariable("id") Integer userId) {
        int res = userService.deleteByPrimaryKey(userId);
        return BooleanTF.judge(res);
    }

    @PutMapping("/update")
    public boolean updateByPrimaryKeySelective(@RequestBody User record) {
        int res = userService.updateByPrimaryKeySelective(record);
        return BooleanTF.judge(res);
    }

    //显示所有信息
    @GetMapping("/selectAll")
    List<User> selectAll() {
        return userService.selectAll();
    }

    //按Id进行查找
    @GetMapping("/selectById/{id}")
    User selectByPrimaryKey(@PathVariable("id") Integer userId) {
        return userService.selectByPrimaryKey(userId);
    }

    //按openId进行查找
    @GetMapping("/selectByOpenId/{openId}")
    User selectByOpenId(@PathVariable("openId") String openId) {
        return userService.selectByOpenId(openId);
    }
}
